import sys
import os
from PySide6.QtWidgets import *
from PySide6.QtGui import *
from PySide6.QtCore import *

class Demo(QWidget):
    _addCount=0
    _insertCount=0
    def __init__(self, parent=None):
        super().__init__(parent)
        self.parent = parent
        layout = QVBoxLayout()
        layout.setAlignment(Qt.AlignmentFlag.AlignTop)
        
        toolBar=QToolBar()
        btn=QPushButton('Add')
        btn.clicked.connect(self.Add)
        toolBar.addWidget(btn)

        btn=QPushButton('Insert')
        btn.clicked.connect(self.Insert)
        toolBar.addWidget(btn)

        btn=QPushButton('Delete')
        btn.clicked.connect(self.Delete)
        toolBar.addWidget(btn)

        btn=QPushButton('MoveUp')
        btn.clicked.connect(self.MoveUp)
        toolBar.addWidget(btn)

        btn=QPushButton('MoveDown')
        btn.clicked.connect(self.MoveDown)
        toolBar.addWidget(btn)

        btn=QPushButton('SelectAll')
        btn.clicked.connect(self.SelectAll)
        toolBar.addWidget(btn)

        btn=QPushButton('ClearSelected')
        btn.clicked.connect(self.ClearSelected)
        toolBar.addWidget(btn)

        btn=QPushButton('ShowSelected')
        btn.clicked.connect(self.ShowSelected)
        toolBar.addWidget(btn)

        layout.addWidget(toolBar)

        self.listView=QListView()
        self.listView.setSelectionMode(QAbstractItemView.SelectionMode.MultiSelection)#多选
        
        self.model=QStringListModel()
        self.model.setStringList(['item'+str(i) for i in range(10)])
        self.listView.setModel(self.model)
        layout.addWidget(self.listView)

        self.setLayout(layout)
    def Add(self):
        self._addCount+=1
        row=self.model.rowCount()
        self.model.insertRow(row)
        index=self.model.index(row)
        text=f'add-{self._addCount}'
        self.model.setData(index,text)
        self.parent.OnShowMessage(f"Add Item '{text}' at {index.row()}")

    def Insert(self):
        index=self.listView.currentIndex()
        if index.row()==-1:return
        self._insertCount+=1        
        text=f'insert-{self._insertCount}'
        self.model.insertRow(index.row())
        self.model.setData(index,text)
        self.parent.OnShowMessage(f"Insert Item '{text}' at {index.row()}")

    def Delete(self):
        index=self.listView.currentIndex()
        if index.row()==-1:return
        text=self.model.data(index)
        self.parent.OnShowMessage(f"Remove Item '{text}' at {index.row()}")
        self.model.removeRow(index.row())
        
    def MoveUp(self):
        index=self.listView.currentIndex()
        if index.isValid()==False:return
        row=index.row()
        if row>0:
            text=self.model.data(index)
            self.model.moveRow(QModelIndex(),row,QModelIndex(),row-1)
            self.parent.OnShowMessage(f"Move Item '{text}' from {row} to {row-1}")
    def MoveDown(self):
        index=self.listView.currentIndex()
        if index.isValid()==False:return
        row=index.row()
        if row<self.model.rowCount()-1:
            text=self.model.data(index)
            self.model.moveRow(QModelIndex(),row+1,QModelIndex(),row)
            self.parent.OnShowMessage(f"Move Item '{text}' from {row} to {row+1}")
    def SelectAll(self):
        self.listView.selectAll()
        self.parent.OnShowMessage(f"Select All Item")
    def ClearSelected(self):
        self.listView.clearSelection()
        self.parent.OnShowMessage(f"Clear Selected")
    def ShowSelected(self):
        selectedText=''
        for index in self.listView.selectedIndexes():
            text=self.model.data(index)
            selectedText+=f'row:{index.row()},text:{text} \n'
        self.parent.OnShowMessage(f"Selected Item:\n{selectedText}")
def runDemo(parent):
    wigdet =  Demo(parent)
    return wigdet